<?php
namespace App\Http\Controllers\Exp;
use App\Http\Controllers\Controller;
use App\Http\Controllers\ApiBaseController;
use Illuminate\Http\Request;
use App\Models\ExpModels\Exp;
class ActivityController extends ApiBaseController{
    /**
	*	爆款码管理模块
	*/
	
	public function __construct(Request $request) {
		parent::__construct($request);
		$this->params = $request->all();
	}
	
	
//	//活动列表
//	function index(){
//		if(!session()->has('vip_user')){
//			return redirect('admin/login');
//		}
//     //   print_r($request->session()->all());exit();
//		return view('exp/index',['exp_view_index'=>true]);
//	}
//
//	//活动列表拉取
//	function info(){
//		$exp=new Exp();
//		$partner_id=isset(session('vip_user')[0]['admin_id']) && intval(session('vip_user')[0]['admin_id']) ? intval(session('vip_user')[0]['admin_id']) : $this->returnJson(1,'请先登录');
//		$title=isset($this->params['title']) ? $this->params['title'] : '';
//		$nextPage=isset($this->params['nextPage']) && intval($this->params['nextPage']) ? intval($this->params['nextPage']) : 1;
//		echo $exp->infoList($partner_id,$title,$nextPage);
//	}
	
	//修改活动状态
	function upStatus(){
		$exp=new Exp();
		$partner_id=isset($this->params['pid']) && intval($this->params['pid']) ? intval($this->params['pid']) : 0;
		$kid=isset($this->params['kid']) && intval($this->params['kid']) ? intval($this->params['kid']) : 0;
		$status=$exp->selStatus($kid,$partner_id);
		$data['updateTime']=date('Y-m-d H:i:s');
		if($status==0){
			$data['status']=1;
		}elseif($status==1){
			$data['status']=0;
		}
		// echo "<pre/>";print_r($status);exit;
		$re=$exp->upStatus($kid,$data);
		$re ? $this->returnJson(0,'操作成功') : $this->returnJson(1,'操作失败');
	}
	
	//删除活动
	function del(){
		$exp=new Exp();
		$kid=isset($this->params['kid']) && intval($this->params['kid']) ? intval($this->params['kid']) : 0;
		$re=$exp->del($kid);
		$re ? $this->returnJson(0,'操作成功') : $this->returnJson(1,'操作失败');
	}
	
	//拉取商户列表
	function userList(){
		$exp=new Exp();
		$partner_id=isset($this->params['pid']) && intval($this->params['pid']) ? intval($this->params['pid']) : 0;
		$thisOpenId=isset($this->params['thisOpenId']) ? $this->params['thisOpenId'] : '';
		$page=isset($this->params['usersNextPage']) && intval($this->params['usersNextPage']) ? intval($this->params['usersNextPage']) : 1;
		$userSreach=isset($this->params['userSreach']) ? $this->params['userSreach'] : '';
		echo $exp->selUsers($partner_id,$thisOpenId,$page,$userSreach);
	}
	
	//视频拉取
	function videoList(){
		$exp=new Exp();
		$partner_id=isset($this->params['pid']) && intval($this->params['pid']) ? intval($this->params['pid']) : 0;
		$videoId=isset($this->params['videoId']) && intval($this->params['videoId'])? intval($this->params['videoId']) : 0;
		$videoSreach=isset($this->params['videoSreach']) ? $this->params['videoSreach'] : '';
		$page=isset($this->params['videosNextPage']) && intval($this->params['videosNextPage']) ? intval($this->params['videosNextPage']) : 1;
		echo $exp->selVideos($partner_id,$videoId,$page,$videoSreach);
	}
	
	//地址搜索高德api
	function addressList(){
		$partner_id=isset($this->params['pid']) && intval($this->params['pid']) ? intval($this->params['pid']) : 0;
		$size=20;
		$url="https://restapi.amap.com/v3/place/text?key={$_ENV['GD_KEY']}&offset={$size}&citylimit=true";
		$keywords=isset($this->params['nameSearch']) ? $this->params['nameSearch'] : '';
		$city=isset($this->params['citySearch']) ? $this->params['citySearch'] : '';
		$page=isset($this->params['addressNextPage']) && intval($this->params['addressNextPage']) ? intval($this->params['addressNextPage']) : 1;
		$poiId=isset($this->params['poiId']) ? $this->params['poiId'] : '';
		
		if($keywords && $city){
			$url.="&keywords={$keywords}&city={$city}&page={$page}";
			$data=$this->GDget($url);
		}
		
		if( isset($data['infocode']) && $data['infocode']=="10000" && !empty($data['pois']) && isset($data['count']) ){
			$html="";
			if($page==1){
				$html.="<tr onclick=\"javascript:$(this).find('input').prop('checked',true);\">";
					$html.="<td style=\"width:50px;\"><input type=\"radio\" ".(!$poiId ? 'checked' : '')." name=\"poiId\" value=\"\" data-address=\"\" data-city=\"\"></td>";
					$html.="<td colspan=\"10\">不选择</td>";
				$html.="<tr>";
			}
			$nextPage=intval($data['count']) > $page * $size ? ++$page : 0;
			foreach($data['pois'] as $k=>$v){
				if( !empty($v['id']) && !empty($v['name']) && !empty($v['address']) && !empty($v['cityname']) ){
					$isChecked=$poiId==$v['id'] ? 'checked' : '';
					$html.="<tr onclick=\"javascript:$(this).find('input').prop('checked',true);\">";
						$html.="<td><input type=\"radio\" {$isChecked} name=\"poiId\" value=\"{$v['id']}\" data-name=\"{$v['name']}\" data-address=\"{$v['address']}\" data-city=\"{$v['cityname']}\"></td>";
						$html.="<td>{$v['name']}</td>";
						$html.="<td>{$v['address']}</td>";
						$html.="<td>{$v['cityname']}</td>";
					$html.="</tr>";
				}
			}
		}elseif( isset($data['infocode']) && $data['infocode']=="10003" ){
			echo json_encode(['code'=>1,'msg'=>"今日已上限，请明日再来"]);exit;
		}elseif( isset($data['infocode']) && $data['infocode']=="10004" ){
			echo json_encode(['code'=>1,'msg'=>"操作过于频繁，请稍后再试"]);exit;
		}else{
			$nextPage=0;
			$html="<tr><td colspan=\"10\">无数据</td></tr>";
		}
		echo json_encode(['code'=>0,'msg'=>$html,'nextPage'=>$nextPage]);exit;
	}
	
	//卡券拉取
	function cardList(){
		$exp=new Exp();
		$partner_id=isset($this->params['pid']) && intval($this->params['pid']) ? intval($this->params['pid']) : 0;
		$cardId=isset($this->params['cardId']) && intval($this->params['cardId'])? intval($this->params['cardId']) : 0;
		$cardSreach=isset($this->params['cardSreach']) ? $this->params['cardSreach'] : '';
		$page=isset($this->params['cardsNextPage']) && intval($this->params['cardsNextPage']) ? intval($this->params['cardsNextPage']) : 1;
		echo $exp->selCards($partner_id,$cardId,$page,$cardSreach);
	}
	
	//添加、修改活动  修改
	function save(){
		$exp=new Exp();
		//print_r($this->params);exit();
		$partner_id=isset($this->params['pid']) && intval($this->params['pid']) ? intval($this->params['pid']) : 0;
		$kid=!empty($this->params['data']['kid']) && intval($this->params['data']['kid']) ? intval($this->params['data']['kid']) : 0;
      //  print_r($partner_id);exit();
		$data['parnterId']=$partner_id;
		$data['title']=isset($this->params['title']) && $this->params['title'] ? htmlspecialchars($this->params['title'],ENT_QUOTES) : $this->returnJson(1,'请填写标题');
		$data['talk']=isset($this->params['talk']) && $this->params['talk'] ? htmlspecialchars($this->params['talk'],ENT_QUOTES) : '';
		$talkArr=explode("#",$this->params['talk']);
		count($talkArr) > 4 ? $this->returnJson(1,'话题最多只能三个') : '';
		$data['openId']=isset($this->params['openId']) && $this->params['openId'] ? htmlspecialchars($this->params['openId'],ENT_QUOTES) : '';
		$data['posInurl']=isset($this->params['posInurl']) && $this->params['posInurl'] ? htmlspecialchars($this->params['posInurl'],ENT_QUOTES) : '';
		$data['poiName']=isset($this->params['poiName']) && $this->params['poiName'] ? htmlspecialchars($this->params['poiName'],ENT_QUOTES) : '';
		$data['poiId']=isset($this->params['poiId']) && $this->params['poiId'] ? htmlspecialchars($this->params['poiId'],ENT_QUOTES) : '';
		$data['poiAddress']=isset($this->params['poiAddress']) && $this->params['poiAddress'] ? htmlspecialchars($this->params['poiAddress'],ENT_QUOTES) : '';
		$data['poiCity']=isset($this->params['poiCity']) && $this->params['poiCity'] ? htmlspecialchars($this->params['poiCity'],ENT_QUOTES) : '';
		$data['videoId']=isset($this->params['videoId']) && intval($this->params['videoId']) ? intval($this->params['videoId']) : 0;
		$data['cardId']=isset($this->params['cardId']) && intval($this->params['cardId']) ? intval($this->params['cardId']) : 0;
		$data['remark']=isset($this->params['remark']) && $this->params['remark'] ? htmlspecialchars($this->params['remark'],ENT_QUOTES) : $this->returnJson(1,'请填写活动介绍');
		
		$catyTime=!empty($this->params['catyTime']) ? $this->params['catyTime'] : $this->returnJson(1,'请选择活动时间');
		$data['staTime']=mb_substr($catyTime,0,19);
		$data['endTime']=mb_substr($catyTime,22);
		$data['updateTime']=date('Y-m-d H:i:s');
		$data['isCheck']=0;
		$data['checkTest']='';
		
	// echo $kid."<pre/>";print_r($data);exit;

		$re=$exp->saveCaty($kid,$data);
		$re ? $this->returnJson(0,'保存成功') : $this->returnJson(1,'保存失败');
	}
	
	
	
	
	
	
	
	//获取数据
    function GDget($url){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_TIMEOUT, 60);
        curl_setopt($ch, CURLOPT_AUTOREFERER, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 跳过证书检查
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); // 不从证书中检查SSL加密算法是否存在
        $res = curl_exec($ch);
        curl_close($ch);
        return json_decode($res,true);
    }
}